<template>
    <div class="sidebar">
        <el-menu :default-active="onRoutes" class="el-menu-vertical-demo" theme="green" unique-opened router>
            <el-menu-item index="readme">
                <!-- <i class="el-icon-setting"></i> -->主页
            </el-menu-item>

            <MyMenu :submenus="priTree" ></MyMenu>

           <!-- <template v-for="menu in menus">
                <el-menu-item :index=menu.address>
                    <i class="el-icon-setting"></i>{{menu.name}}
                </el-menu-item>
            </template> -->

            <!-- <el-submenu index="3">
                <template slot="title"><i class="el-icon-menu"></i>表格</template>
                <el-menu-item index="basetable">基础表格</el-menu-item>
                <el-menu-item index="vuetable">Vue表格组件</el-menu-item>
            </el-submenu>-->
            <!-- <el-submenu index="4"> 
                <template slot="title"><i class="el-icon-menu"></i>商城管理</template>
                 <el-menu-item index="WithdrawSetting">提现设置</el-menu-item>
                 <el-menu-item index="withdrawManage">提现管理</el-menu-item>
                 <el-menu-item index="withdrawList">提现列表</el-menu-item>
                 <el-menu-item index="serviceComplaints">客服投诉表</el-menu-item>
                 <el-menu-item index="FeedbackList">意见反馈列表</el-menu-item> 
                 <el-menu-item index="advertisingManage">广告图管理</el-menu-item> 
            </el-submenu>
            <el-submenu index="5"> 
                <template slot="title"><i class="el-icon-menu"></i>二手市场管理</template>
                 <el-menu-item index="secondhand">二手分类设置</el-menu-item>
                 <el-menu-item index="secondhandList">二手商品管理</el-menu-item>
                 <el-menu-item index="user">广告图管理</el-menu-item> 
            </el-submenu>
            <el-submenu index="5"> 
                <template slot="title"><i class="el-icon-menu"></i>销售管理</template>
                 <el-menu-item index="WithdrawSetting">订单管理</el-menu-item>
                 <el-menu-item index="privilegeManage">发货</el-menu-item>
                 <el-menu-item index="role">退货请求</el-menu-item> 
                 <el-menu-item index="user">流水记录</el-menu-item> 
            </el-submenu> -->
        </el-menu>
    </div>
</template>

<script>
import { withdrawListData } from '../../utils/resourse';
import { mapState } from 'vuex'
export default {

    mounted: function() {
        // console.log(withdrawListData)
        var _self = this;
        // var data = {
        //     rid: ''
        // };
        _self.myajax("/privilege/getPrivilegeByCurrentRoleId", {}, function(data) {
            _self.menus = data.data.data;
             console.log(data)
             _self.userToken=data.data.rid
             localStorage.setItem('userToken', data.data.rid);
             localStorage.setItem('rid', data.data.rid);
             localStorage.setItem('school', JSON.stringify(data.data.school));
             localStorage.setItem('shop', JSON.stringify(data.data.shop));
             localStorage.setItem('userData', JSON.stringify(data.data.user));
             _self.$store.commit('getToken')
            _self.priTree = _self.beTree(new Array.concat(data.data.data));
        });
        // _self.myajax("/shop/getShopId", null, function(data) {
        //     localStorage.setItem('shopId', data.data);
        // });
        // _self.beTree(_self.priTree);

        // if(_self.menus&&_self.menus[0].id=='b54eb2bdfaa74625b5bb0c0c50f4168d'){
        //     localStorage.setItem('justResister',"yes");
        // }else{
        //     localStorage.setItem('justResister',"no");
        // }

    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        },
    },
    data: function() {
        return {
            menus: [],
            treeMenus:[],
            priTree:[],
            userToken:""
        }
    },
    components: {
        'MyMenu':{
            name: 'MyMenu',
            template: '<div><template v-for="submenu in submenus">'
                +'<el-submenu  :index="submenu.id" v-if="submenu.children&&submenu.children.length>0">'
                +'<template slot="title"><!--<i class="el-icon-menu"></i>-->{{submenu.name}}</template><MyMenu :submenus="submenu.children" ></MyMenu>'
                +'</el-submenu>'
                +'<el-menu-item v-else :index="submenu.address==null?submenu.id:submenu.address">{{submenu.name}}</el-menu-item>'
                +'</template></div>',
            props: ['submenus'],
            computed: {
                /*submenusLen: function (submenu) {
                    if(submenu&&submenu.children&&submenu.children.length>0){
                        return true;
                    }
                    return false;
                }*/
            }
        },
    },
    methods:{
        beTree(data){
          for(var k in data){
            var bdata=data[k];
            var id=bdata.id;
            genTree(id,bdata,data);
          }
          function genTree(pid,bdata,data){
            for(var k in data){
              var gdata=data[k];
              if(gdata.pid&&gdata.pid==pid&&pid!=gdata.id){
                gdata.self_issub=true;
                if(bdata.children){
                  bdata.children.push(gdata);
                }else{
                  bdata.children=new Array();
                  bdata.children.push(gdata);
                }
              }
            }
          }
          for(var i=data.length-1;i>=0;i--){
            var bdata=data[i];
            if(bdata.self_issub){
              data.splice(i,1);
            }
          }
          return data;
        },
    }
}
</script>
<style scoped>
.sidebar {
    display: block;
    position: absolute;
    width: 250px;
    left: 0;
    top: 70px;
    bottom: 0;
    background: #2E363F;
}

.sidebar > ul {
    height: 100%;
}
/**   background-color: rgb(27, 171, 166);  66 176 163***/
</style>
